package com.zwl.sjzmanagement.aop;

import com.atguigu.tingshu.common.result.Result;
import com.zwl.model.LoginInfo;
import com.zwl.model.UserInfo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Aspect
public class LoginLogAspect {


    @Around("execution(* com.zwl.sjzmanagement.controller.WxLoginApiController.Login(..))")
    public Object recordLoginLog(ProceedingJoinPoint pjp) throws Throwable {
        //用户名
        Object[] args = pjp.getArgs();
        UserInfo e = (UserInfo) args[0];
        String username = e.getUsername();
        //密码
        String password = e.getPassword();
        //登录时间
        LocalDateTime loginTime = LocalDateTime.now();

        long begin = System.currentTimeMillis();
        //调用原始方法
        Object obj = pjp.proceed();
        long end = System.currentTimeMillis();

        Result result = (Result) obj;

        //获取登录是否成功
        Short isSuccess = result.getCode().shortValue();
        //jwt
        String jwt = "";
        if (isSuccess == 1){ //成功
            Object data = result.getData();
            LoginInfo loginInfo = (LoginInfo) data;
            jwt = loginInfo.getToken();
        }
        long duration = end - begin;

        System.out.println(jwt+"0000:"+duration);

        return obj;
    }

}
